package com.inspur.his.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.inspur.his.common.EncrypDES;
import com.inspur.his.pojo.User;
import com.inspur.his.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import javax.servlet.http.HttpServletRequest;
import java.text.SimpleDateFormat;
import java.util.*;
import com.inspur.his.constant.Constant;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
public class UserController {
    @Autowired
    UserService userService;

    @RequestMapping("updatepdwContrller")
    public String updatepwd(String id, String username, String password2) throws Exception {
        HashMap map =  new HashMap();
        map.put("id",id);
        String key = "2020@#$2020";
        EncrypDES des = new EncrypDES(key);
        String en_pass = des.encrypt(password2);//加密后的密码
        map.put("password",en_pass);
        map.put("password",en_pass);
        userService.updatePwd(map);
        return "login.jsp";
    }
    @RequestMapping("userListController")
    public String userList(HttpServletRequest request, Model model, @RequestParam(value = "pn", defaultValue = "1") Integer pn){
        //获取页面提交来的查询条件
        String username = request.getParameter("username");
        String role = request.getParameter("role");
        //  点击用户管理左侧菜单进行默认无条件查询
        HashMap map = new HashMap();
        map.put("username",username);
        map.put("role",role);
        PageHelper.startPage(pn,5);
        List<User> userList = userService.userList(map);
        PageInfo<User> pageInfo = new PageInfo<User>(userList,userList.size());
        model.addAttribute("userlist",userList);
        model.addAttribute("pageInfo",pageInfo);
        model.addAttribute("susername",username);
        model.addAttribute("srole",role);
        return "user/userList.jsp";
    }
    @RequestMapping("userAddController")
    public String userAdd(User user,Model model) throws Exception {
        //参数绑定 user对象中已经绑定了表单中的用户信息(确认sql中插入的所有字段均有值吗？）
        //思考主键的生成方式--多种1 MySQL中的主键自增
        String id = UUID.randomUUID().toString();
        user.setId(id);
        //user.setStatus("1");//公用的参数值尽量不要使用写死的数据，采用静态常量的方式，此为写死状态
        user.setStatus(Constant.USER_STATUS_ABLE);
        Date day = new Date();
        SimpleDateFormat fm = new SimpleDateFormat("yyyy-MM-DD");
        String today = fm.format(day);
        user.setCreatedate(fm.parse(today));
        //密码加密
        String key = "2020@#$2020";
        EncrypDES des = new EncrypDES(key);
        String en_pass = des.encrypt(user.getPassword());//加密后的密码
        user.setPassword(en_pass);
        userService.userAdd(user);
        model.addAttribute("user",user);
        model.addAttribute("flag",Constant.USER_STATUS_ABLE);
        model.addAttribute("success","用户信息保存成功！");
        return "user/userAdd.jsp";
    }
    @RequestMapping("/modifyUserController")
    public String modifyUser(String id, Model model) {
        User user = userService.getUserById(id);
        model.addAttribute("user", user);
        return "/user/userModify.jsp";
    }
    @RequestMapping("/userUpdateController")
    public String userupdate(User user, Model model) {
        userService.updateUser(user);
        model.addAttribute("success", "用户信息修改成功");
        model.addAttribute("user", user);
        model.addAttribute("flag", "1");
        return "/user/userModify.jsp";
    }

    @RequestMapping("/checkUser")
    @ResponseBody
    public Map checkUser(String username, Model model) {
        List<User> userList = userService.getUserByName(username);
        Map map = new HashMap();
        map.put("userList", userList);
        if (userList.size() >= 1) {
            map.put("msg", "已存在该用户");
        }
        return map;
    }
    @RequestMapping("/checkoldPassword")
    @ResponseBody
    public Map checkoldPassword(String oldpassword,String id) throws Exception {
        User user = userService.getUserById(id);
        String db_password = "";
        if(user != null){
            db_password = user.getPassword();
        }
        String key = "2020@#$2020";
        EncrypDES des = new EncrypDES(key);
        String en_pass = des.encrypt(oldpassword);//加密后的密码
        Map map = new HashMap();
        if(!en_pass.equals(db_password)){
            map.put("msg", "原密码不正确！");
        }
        return map;
    }
    @RequestMapping("changeStatus")
    public String changeStatus(HttpServletRequest req, Model md,
                               @RequestParam(value = "pn", defaultValue = "1") Integer pn) {
        // 获取页面传递过来的查询条件
        String username = req.getParameter("username");
        String role = req.getParameter("role");
        // 页面查询条件查询后锁定显示在页面上
        md.addAttribute("username", username);
        md.addAttribute("role", role);

        String id = req.getParameter("id");
        String sts = req.getParameter("sts");
        HashMap map = new HashMap<String, String>();
        map.put("id", id);
        map.put("status", sts);
        userService.changeStatus(map);
        return "redirect:userListController";
    }
}